package com.wd.live.viewmodel

import HttpUtils
import android.util.Log
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.wd.live.model.base.BaseViewModel
import com.wd.live.model.bean.CloseLiveBean
import com.wd.live.model.bean.LM_QuerylistBean
import com.wd.live.model.bean.LocalData
import com.wd.live.model.bean.LookBean
import com.wd.live.model.bean.OpenLiveBean
import com.wd.live.model.bean.TuiBean
import com.wd.live.model.bean.UserShenQingBean
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

/**
 * 作者：马海钊
 * 时间：2023/8/22 17:11
 * 功能：开播、看播、推流、关流、连麦
 */
class OpenLiveViewModel : BaseViewModel() {
    //开直播
    var mLiveBeanMediatorLiveData: MutableLiveData<OpenLiveBean> = MediatorLiveData<OpenLiveBean>()
    fun getOpenLive(file: File) {
        var requestBody: RequestBody = RequestBody
            .create("multipart/form-data".toMediaTypeOrNull(), file)

        var liveCoverImage: MultipartBody.Part =
            MultipartBody.Part.createFormData("liveCoverImage", file.name, requestBody)

        HttpUtils.getInstance().api.openlive(
            101, "天上的星星不说话",
            "116.40400", "39.92800", liveCoverImage
        )
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                mLiveBeanMediatorLiveData.postValue(it)
            }, { error ->
                Log.d("getOpenLiveError", error.message.toString())
            })
    }

    //看播
    public var lookLivevideoLiveData = MutableLiveData<LookBean.Result>()
    fun looklive(liveId: Int) {
        HttpUtils.getInstance().api.looklive(liveId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                lookLivevideoLiveData.postValue(it.result)
            }, { error ->
                Log.d("lookliveError", error.message.toString())
            })
    }

    //用户连麦
    public var UserShenQingLiveData = MutableLiveData<UserShenQingBean>()
    fun userlianmai(liveId: Int) {
        HttpUtils.getInstance().api.usershenqing(liveId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                UserShenQingLiveData.postValue(it)
            }, { error ->
                Log.d("userlianmaiError", error.message.toString())
            })
    }

    //主播查询连麦列表
    public var LM_QueryLiveData = MutableLiveData<LM_QuerylistBean>()
    fun zbquerylist(liveid: Int) {
        HttpUtils.getInstance().api.zbquerylist(liveid)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                LM_QueryLiveData.postValue(it)
            }, { error ->
                Log.d("userlianmaiError", error.message.toString())
            })
    }

    //推流
    public var startPushStreamLiveData = MutableLiveData<TuiBean>()
    fun startPushStream(liveId: Int) {
        HttpUtils.getInstance().api.startPushStream(liveId)
            ?.subscribeOn(Schedulers.io())
            ?.observeOn(AndroidSchedulers.mainThread())
            ?.subscribe ({
                startPushStreamLiveData.postValue(it)
            }, { error ->
                Log.d("userlianmaiError", error.message.toString())
            })
    }

    //关流
    public var closePushStreamLiveData = MutableLiveData<CloseLiveBean>()
    fun closePushStream() {
        HttpUtils.getInstance().api.closePushStream()
            ?.subscribeOn(Schedulers.io())
            ?.observeOn(AndroidSchedulers.mainThread())
            ?.subscribe ({
                closePushStreamLiveData.postValue(it)
            }, { error ->
                Log.d("userlianmaiError", error.message.toString())
            })
    }

    //查询礼物列表
    public var queryGiftLiveData=MutableLiveData<LocalData.QueryGiftBean>()
    fun queryGiftlist(){
        HttpUtils.getInstance().api.queryGiftList()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe ({
                queryGiftLiveData.postValue(it)
            }, { error ->
                Log.d("queryGiftLiveDataError", error.message.toString())
            })
    }
    //送礼物
    public var sendgiftLiveData=MutableLiveData<LocalData.SendGiftBean>()
    fun sendgift(giftId:Int,liveId:Int){
        HttpUtils.getInstance().api.sendGift(giftId, liveId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe ({
                sendgiftLiveData.postValue(it)
                ToastUtils.showLong(it.message)
            }, { error ->
                Log.d("sendgiftLiveDataError", error.message.toString())
            })
    }
}